package pe.com.bbva.ge.vista.vaadin.ui.component;

import java.util.HashMap;
import java.util.Map;

import pe.com.bbva.ge.vista.vaadin.ui.utileria.ComboValueChangeListener;

import com.cdainfo.vaadin.spring.Inject;
import com.vaadin.ui.ComboBox;

public abstract class ComboBoxGE extends ComboBox {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5094083174948529148L;
	
	private Map<ComboValueChangeListener, ValueChangeListener> listeners;
	
	public ComboBoxGE() {
		super();
		Inject.inject(this);
		listeners = new HashMap<ComboValueChangeListener, ValueChangeListener>();
	}
	
	public void addComboValueChangeListener(final ComboValueChangeListener listener) {
		
		ValueChangeListener valueChangeListener = new ValueChangeListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 5261882770994315583L;

			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				listener.comboValueChange(obtenerComboBox(), event);
			}
		};
		listeners.put(listener, valueChangeListener);
		
		addListener(valueChangeListener);
		
    }
	
	public void removeComboValueChangeListener(final ComboValueChangeListener listener) {
		ValueChangeListener valueChangeListener = listeners.get(listener);
		removeListener(valueChangeListener);
	}
	
	private ComboBoxGE obtenerComboBox()
	{
		return this;
	}
	
	@Override
	public void attach() {
		super.attach();
		postConstruccion();
	}

	protected abstract void postConstruccion();
	
}
